const cheerio = require('cheerio');
const HOUSE_TYPE = require('../constant/index');

class Core {
	constructor() {
		this.name = 'core';
	}

	/**
	 * 核心解析
	 * @param {*} html
	 * @param {*} rules
	 * @returns
	 */
	pause(type = HOUSE_TYPE.ER_SHOU_FANG, html, rules = {}) {
		const list = [];
		const $ = cheerio.load(html);

		// 挂牌二手房
		if (type === HOUSE_TYPE.ER_SHOU_FANG) {
			$('.sellListContent li').each((i, item) => {
				const title = $(item).find('.title > a[title]').attr('title');
				const address = $(item).find('.address .flood a').text();
				const addressUrl = $(item).find('.address .flood a').attr('href');
				const houseInfo = $(item).find('.houseInfo').text().trim().replace(/\s+/g, '').split('|');
				const followInfo = $(item).find('.followInfo').text().trim().replace(/\s+/g, '').split('/');
				const price = $(item).find('.address .priceInfo .unitPrice span').text().replace(/\s+/g, '');
				const totalPrice = $(item).find('.address .priceInfo .totalPrice span').text().replace(/\s+/g, '');
				const tags = [];

				$(item).find('.tag span').each((i, iv) => {
					tags.push($(iv).text());
				});

				let [houseDescText, houseAreaText, houseDirText] = houseInfo;

				houseAreaText = houseAreaText?.replace(/平米/g, '');

				let [careInfo, publishDate] = followInfo;

				const result = {
					title,
					address,
					addressUrl,
					houseDescText,
					houseAreaText,
					houseDirText,
					careInfo,
					publishDate,
					price: price?.replace(/元\/平/g, '').replace(/,/g, ''),
					totalPrice,
					tags
				};

				list.push(result);
			});
		}

		// 新房
		if (type === HOUSE_TYPE.XIN_FANG) {
			$('.resblock-list-wrapper li').each((i, item) => {
				const loupanName = $(item).find('.resblock-name>h2>a').attr('title');
				const loupanState = $(item).find('.resblock-name .resblock-type').text();
				const loupanType = $(item).find('.resblock-name span').last().text();
				const loupanLocation = $(item).find('.resblock-location').text().replace(/\s+/g, '');
				const [loupanArea, loupanTown, loupanStreet] = loupanLocation.split('/');
				let roomInfo = [];

				$(item).find('a.resblock-room span').each((i, iv) => {
					roomInfo.push($(iv).text());
				});

				let tags = [];

				$(item).find('.resblock-tag span').each((i, item) => {
					const text = $(item).text();

					tags.push(text);
				});

				roomInfo = roomInfo.filter((_, index) => index > 0);

				const price = $(item).find('.resblock-price .number').text();
				const totalPrice = $(item).find('.second').text().replace(/总价/g, '');

				const result = {
					loupanName,
					loupanState,
					loupanType,
					loupanArea,
					loupanTown,
					loupanStreet,
					roomInfo,
					tags,
					price,
					totalPrice
				};

				list.push(result);
			});
		}

		// 二手房成交
		if (type === HOUSE_TYPE.ER_SHOU_FANG_CHENG_JIAO) {
			$('.listContent li').each((i, row) => {
				const title = $(row).find('.info .title a').text().replace(/\s+/g, '');
				const target = $(row).find('a').attr('href');
				const img = $(row).find('a > .lj-lazy').attr('data-original');
				const address = $(row).find('.info > .address > .houseInfo').text().replace(/\s+/g, '');
				const houseInfo = address.split('|');
				const direction = houseInfo[0];
				const style = houseInfo[1];
				const dealDate = $(row).find('.info > .address > .dealDate').text().replace(/\s+/g, '');
				const position = $(row).find('.info > .flood > .positionInfo').text().replace(/\s+/g, '');
				const dealCycleeInfo = $(row).find('.info > .dealCycleeInfo > .dealCycleTxt').text().replace(/\s+/g, '');
				const totalPrice = $(row).find('.info > .address > .totalPrice > .number').text().replace(/\s+/g, '');

				const result = {
					title,
					img,
					target,
					direction,
					style,
					dealDate,
					position,
					dealCycleeInfo,
					totalPrice
				};

				list.push(result);
			});
		}

		return list;
	}
}

module.exports = Core;